/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmti.skeleton.service.impl;

import com.cmti.skeleton.dao.mapper.SysDeptMapper;
import com.cmti.skeleton.dao.mapper.SysUserMapper;
import com.cmti.skeleton.dto.SysDeptDto;
import com.cmti.skeleton.dto.TreeNode;
import com.cmti.skeleton.dto.TreeNodeLayui;
import com.cmti.skeleton.entity.SysDept;
import com.cmti.skeleton.service.DeptService;
import com.cmti.skeleton.service.SysLogService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Worker
 */
@Service
@Transactional(readOnly = true)
public class DeptServiceImpl implements DeptService {

    private final Log log = LogFactory.getLog(this.getClass());
    @Autowired
    SysDeptMapper deptMapper;
    @Autowired
    SysUserMapper userMapper;
    @Autowired
    SysLogService sysLogService;

    /**
     * 获取顶级部门
     *
     * @param sysDeptDto
     * @return 成功则返回list集合，失败返回空集合
     *
     */
    @Override
    public List<SysDeptDto> getDepartmentList(SysDeptDto sysDeptDto) {

//        StringBuilder sql = new StringBuilder("SELECT * FROM sys_dept WHERE STATUS=0 AND P_ID=0");
        //如果按名称来搜索
//        if (sysDeptDto.getName() != null && sysDeptDto.getName().length() > 0) {
//            sql = new StringBuilder("SELECT * FROM sys_dept WHERE STATUS=0 AND NAME='" + sysDeptDto.getName() + "'");
//        }
//        List<SysDeptDto> firstLevelDepts = dao.find(SysDeptDto.class, sql.toString());
        List<SysDeptDto> firstLevelDepts = deptMapper.selectByParentId(0);
        for (SysDeptDto dept : firstLevelDepts) {

            this.buildDeptTree(dept);
        }
        return firstLevelDepts;
    }

    /**
     * 构建部门树
     *
     * @param dept
     */
    private void buildDeptTree(SysDeptDto dept) {
        List<SysDeptDto> subDepts = deptMapper.selectByParentId(dept.getId());
        dept.setSubDepts(subDepts);
        for (SysDeptDto subDept : subDepts) {
            buildDeptTree(subDept);
        }
    }

    /**
     * 构建部门层次json
     *
     * @param deptList
     * @param treeNodeList
     */
    public void buildDeptJson(List<SysDeptDto> deptList, List<TreeNode> treeNodeList) {
        for (SysDeptDto dept : deptList) {
            TreeNode treeNode = new TreeNode();
            //定义map用于增加附加的属性
            Map<String, Object> attributes = new HashMap<>();
            treeNode.setId(dept.getId());
            treeNode.setText(dept.getName());
            attributes.put("pId", dept.getPId());
            attributes.put("remark", dept.getRemark());
            attributes.put("iconCls", "icon-door-in");
            treeNode.setAttributes(attributes);
            treeNodeList.add(treeNode);

            for (SysDeptDto subDept : dept.getSubDepts()) {
                List<TreeNode> children = new ArrayList<>();
                treeNode.setChildren(children);
                buildDeptJson(dept.getSubDepts(), children);
            }
        }
    }

    /**
     * 增加部门
     *
     * @param sysDept
     * @return 成功返回1
     */
    @Transactional
    @Override
    public long addDept(SysDept sysDept) {
        return deptMapper.insertSelective(sysDept);
    }

    /**
     * 修改部门
     *
     * @param sysDept
     * @return ifUpdate 成功返回true,失败返回false
     */
    @Transactional
    @Override
    public boolean updateDept(SysDept sysDept) {
        return deptMapper.updateByPrimaryKeySelective(sysDept) == 1;
    }

    /**
     * 删除部门 以及其子部门
     *
     * @param deptId
     * @return ifDel 返回影响的行数
     */
    @Transactional
    @Override
    public long delDept(long deptId) {
        // 先删除子部门
        deptMapper.deleteByParentId(deptId);
        // 删除该部门
        return deptMapper.deleteByPrimaryKey(deptId);
    }

    /**
     * 获取树形结构的部门列表
     *
     * @return
     */
    @Override
    public List<TreeNode> getDeptTree() {
        List<SysDeptDto> firstLevelDepts = deptMapper.selectByParentId(0L);
        for (SysDeptDto dept : firstLevelDepts) {
            this.buildDeptTree(dept);
        }
        List<TreeNode> treeNodeList = new ArrayList<>();
        buildDeptJson(firstLevelDepts, treeNodeList);
        return treeNodeList;
    }

    @Override
    public boolean hasAssociatedUsers(long deptId) {
        return userMapper.countByDepartmentId(deptId) > 0;
    }

    @Override
    public boolean hasSubDepartments(long deptId) {
        return deptMapper.countByParentId(deptId) > 0;
    }

    @Override
    public SysDept getDeptById(long deptId) {
        return deptMapper.selectById(deptId);
    }

    @Override
    public List<TreeNodeLayui> getDeptTreeList() {
        List<SysDeptDto> selectByParentId = deptMapper.selectByParentId(0L);//获取父节点
        for (SysDeptDto sysDeptDto : selectByParentId) {
            this.buildDeptTree(sysDeptDto);
        }
        List<TreeNodeLayui> treeNodeList = new ArrayList<>();
        buildDeptTree(selectByParentId, treeNodeList);
        return treeNodeList;
    }

    public void buildDeptTree(List<SysDeptDto> deptList, List<TreeNodeLayui> treeNodeList) {
        for (SysDeptDto dept : deptList) {
            TreeNodeLayui treeNode = new TreeNodeLayui();
            treeNode.setId(dept.getId());
            treeNode.setName(dept.getName());
            treeNodeList.add(treeNode);

            for (SysDeptDto subDept : dept.getSubDepts()) {
                List<TreeNodeLayui> children = new ArrayList<>();
                treeNode.setChildren(children);
                buildDeptTree(dept.getSubDepts(), children);
            }
        }
    }

    @Override
    public List<SysDeptDto> getListWithParentName() {
        return deptMapper.selectWithParentDeptName();
    }
}
